'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const S = require('../nodes/Scalar.cjs'),
  b = require('./foldFlowLines.cjs'),
  E = (i, n) => ({
    indentAtStart: n ? i.indent.length : i.indentAtStart,
    lineWidth: i.options.lineWidth,
    minContentWidth: i.options.minContentWidth,
  }),
  D = i => /^(%|---|\.\.\.)/m.test(i);
function N(i, n, e) {
  if (!n || n < 0) return !1;
  const l = n - e,
    a = i.length;
  if (a <= l) return !1;
  for (let r = 0, t = 0; r < a; ++r)
    if (
      i[r] ===
      `
`
    ) {
      if (r - t > l) return !0;
      if (((t = r + 1), a - t <= l)) return !1;
    }
  return !0;
}
function y(i, n) {
  const e = JSON.stringify(i);
  if (n.options.doubleQuotedAsJSON) return e;
  const { implicitKey: l } = n,
    a = n.options.doubleQuotedMinMultiLineLength,
    r = n.indent || (D(i) ? '  ' : '');
  let t = '',
    f = 0;
  for (let s = 0, o = e[s]; o; o = e[++s])
    if (
      (o === ' ' &&
        e[s + 1] === '\\' &&
        e[s + 2] === 'n' &&
        ((t += e.slice(f, s) + '\\ '), (s += 1), (f = s), (o = '\\')),
      o === '\\')
    )
      switch (e[s + 1]) {
        case 'u':
          {
            t += e.slice(f, s);
            const u = e.substr(s + 2, 4);
            switch (u) {
              case '0000':
                t += '\\0';
                break;
              case '0007':
                t += '\\a';
                break;
              case '000b':
                t += '\\v';
                break;
              case '001b':
                t += '\\e';
                break;
              case '0085':
                t += '\\N';
                break;
              case '00a0':
                t += '\\_';
                break;
              case '2028':
                t += '\\L';
                break;
              case '2029':
                t += '\\P';
                break;
              default:
                u.substr(0, 2) === '00'
                  ? (t += '\\x' + u.substr(2))
                  : (t += e.substr(s, 6));
            }
            (s += 5), (f = s + 1);
          }
          break;
        case 'n':
          if (l || e[s + 2] === '"' || e.length < a) s += 1;
          else {
            for (
              t +=
                e.slice(f, s) +
                `

`;
              e[s + 2] === '\\' && e[s + 3] === 'n' && e[s + 4] !== '"';

            )
              (t += `
`),
                (s += 2);
            (t += r), e[s + 2] === ' ' && (t += '\\'), (s += 1), (f = s + 1);
          }
          break;
        default:
          s += 1;
      }
  return (
    (t = f ? t + e.slice(f) : e),
    l ? t : b.foldFlowLines(t, r, b.FOLD_QUOTED, E(n, !1))
  );
}
function Q(i, n) {
  if (
    n.options.singleQuote === !1 ||
    (n.implicitKey &&
      i.includes(`
`)) ||
    /[ \t]\n|\n[ \t]/.test(i)
  )
    return y(i, n);
  const e = n.indent || (D(i) ? '  ' : ''),
    l =
      "'" +
      i.replace(/'/g, "''").replace(
        /\n+/g,
        `$&
${e}`,
      ) +
      "'";
  return n.implicitKey ? l : b.foldFlowLines(l, e, b.FOLD_FLOW, E(n, !1));
}
function h(i, n) {
  const { singleQuote: e } = n.options;
  let l;
  if (e === !1) l = y;
  else {
    const a = i.includes('"'),
      r = i.includes("'");
    a && !r ? (l = Q) : r && !a ? (l = y) : (l = e ? Q : y);
  }
  return l(i, n);
}
let B;
try {
  B = new RegExp(
    `(^|(?<!
))
+(?!
|$)`,
    'g',
  );
} catch {
  B = /\n+(?!\n|$)/g;
}
function k({ comment: i, type: n, value: e }, l, a, r) {
  const { blockQuote: t, commentString: f, lineWidth: s } = l.options;
  if (!t || /\n[\t ]+$/.test(e) || /^\s*$/.test(e)) return h(e, l);
  const o = l.indent || (l.forceBlockIndent || D(e) ? '  ' : ''),
    u =
      t === 'literal'
        ? !0
        : t === 'folded' || n === S.Scalar.BLOCK_FOLDED
          ? !1
          : n === S.Scalar.BLOCK_LITERAL
            ? !0
            : !N(e, s, o.length);
  if (!e)
    return u
      ? `|
`
      : `>
`;
  let g, d;
  for (d = e.length; d > 0; --d) {
    const O = e[d - 1];
    if (
      O !==
        `
` &&
      O !== '	' &&
      O !== ' '
    )
      break;
  }
  let c = e.substring(d);
  const F = c.indexOf(`
`);
  F === -1
    ? (g = '-')
    : e === c || F !== c.length - 1
      ? ((g = '+'), r && r())
      : (g = ''),
    c &&
      ((e = e.slice(0, -c.length)),
      c[c.length - 1] ===
        `
` && (c = c.slice(0, -1)),
      (c = c.replace(B, `$&${o}`)));
  let $ = !1,
    L,
    w = -1;
  for (L = 0; L < e.length; ++L) {
    const O = e[L];
    if (O === ' ') $ = !0;
    else if (
      O ===
      `
`
    )
      w = L;
    else break;
  }
  let p = e.substring(0, w < L ? w + 1 : L);
  p && ((e = e.substring(p.length)), (p = p.replace(/\n+/g, `$&${o}`)));
  let _ = (u ? '|' : '>') + ($ ? (o ? '2' : '1') : '') + g;
  if ((i && ((_ += ' ' + f(i.replace(/ ?[\r\n]+/g, ' '))), a && a()), u))
    return (
      (e = e.replace(/\n+/g, `$&${o}`)),
      `${_}
${o}${p}${e}${c}`
    );
  e = e
    .replace(
      /\n+/g,
      `
$&`,
    )
    .replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2')
    .replace(/\n+/g, `$&${o}`);
  const T = b.foldFlowLines(`${p}${e}${c}`, o, b.FOLD_BLOCK, E(l, !0));
  return `${_}
${o}${T}`;
}
function U(i, n, e, l) {
  const { type: a, value: r } = i,
    {
      actualString: t,
      implicitKey: f,
      indent: s,
      indentStep: o,
      inFlow: u,
    } = n;
  if (
    (f &&
      r.includes(`
`)) ||
    (u && /[[\]{},]/.test(r))
  )
    return h(r, n);
  if (
    !r ||
    /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(
      r,
    )
  )
    return f ||
      u ||
      !r.includes(`
`)
      ? h(r, n)
      : k(i, n, e, l);
  if (
    !f &&
    !u &&
    a !== S.Scalar.PLAIN &&
    r.includes(`
`)
  )
    return k(i, n, e, l);
  if (D(r)) {
    if (s === '') return (n.forceBlockIndent = !0), k(i, n, e, l);
    if (f && s === o) return h(r, n);
  }
  const g = r.replace(
    /\n+/g,
    `$&
${s}`,
  );
  if (t) {
    const d = $ => {
        var L;
        return (
          $.default &&
          $.tag !== 'tag:yaml.org,2002:str' &&
          ((L = $.test) == null ? void 0 : L.test(g))
        );
      },
      { compat: c, tags: F } = n.doc.schema;
    if (F.some(d) || (c != null && c.some(d))) return h(r, n);
  }
  return f ? g : b.foldFlowLines(g, s, b.FOLD_FLOW, E(n, !1));
}
function A(i, n, e, l) {
  const { implicitKey: a, inFlow: r } = n,
    t =
      typeof i.value == 'string'
        ? i
        : Object.assign({}, i, { value: String(i.value) });
  let { type: f } = i;
  f !== S.Scalar.QUOTE_DOUBLE &&
    /[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(t.value) &&
    (f = S.Scalar.QUOTE_DOUBLE);
  const s = u => {
    switch (u) {
      case S.Scalar.BLOCK_FOLDED:
      case S.Scalar.BLOCK_LITERAL:
        return a || r ? h(t.value, n) : k(t, n, e, l);
      case S.Scalar.QUOTE_DOUBLE:
        return y(t.value, n);
      case S.Scalar.QUOTE_SINGLE:
        return Q(t.value, n);
      case S.Scalar.PLAIN:
        return U(t, n, e, l);
      default:
        return null;
    }
  };
  let o = s(f);
  if (o === null) {
    const { defaultKeyType: u, defaultStringType: g } = n.options,
      d = (a && u) || g;
    if (((o = s(d)), o === null))
      throw new Error(`Unsupported default string type ${d}`);
  }
  return o;
}
exports.stringifyString = A;
